package com.jx.mlxg.controller;

<<<<<<< HEAD
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jx.mlxg.entity.*;
import com.jx.mlxg.mapper.AddressesMapper;
import com.jx.mlxg.mapper.MaterialRequestMapper;
import com.jx.mlxg.mapper.ShippingInfoMapper;
import com.jx.mlxg.service.*;
import com.jx.mlxg.service.impl.MaterialRequestServiceImpl;
import com.jx.mlxg.service.impl.OrdersServiceImpl;
import com.jx.mlxg.utils.HttpClientUtil;
import com.jx.mlxg.utils.PageResponse;
import com.jx.mlxg.utils.PageSearchRequest;
import com.jx.mlxg.utils.ResponseResult;
import com.jx.mlxg.utils.play.WxPlay;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/materialrequest")
public class MaterialRequestController {
    @Autowired
    MaterialRequestService materialRequestService;

    @Autowired
    OrdersService ordersService;

    @Resource
    AddressesService addressesService;

    @Resource
    UsersService  usersService;

    @Resource
    ShippingInfoService  shippingInfoService;

    @Autowired
    private ApprovalRecordService approvalRecordService;

    @Resource
    private ShippingInfoMapper shippingInfoMapper;

    @Autowired
    private AddressesMapper addressesMapper;

    @PostMapping("/save")
    public ResponseResult save(@RequestBody MaterialRequest materialRequest) {
        log.info("【发货申请】开始处理新增发货请求，请求参数：{}", materialRequest.toString());

        String orderId = null;
        try {
            log.debug("【订单生成】开始创建订单，发货申请ID：{}", materialRequest.getRequestId());
            orderId = ordersService.createOrderByRequestId(materialRequest);

            if (orderId == null) {
                log.error("【订单生成】订单创建失败，返回空订单ID");
                return ResponseResult.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), "订单生成失败");
            }
            log.info("【订单生成】成功创建订单，订单ID：{}", orderId);

            log.debug("【地址查询】开始查询地址信息，地址ID：{}", materialRequest.getAddressId());
            Addresses addresses = addressesMapper.selectById(materialRequest.getAddressId());
            if (addresses == null) {
                log.error("【地址查询】地址信息不存在，地址ID：{}", materialRequest.getAddressId());
                return ResponseResult.failure(HttpStatus.BAD_REQUEST.value(), "地址信息不存在");
            }

            ShippingInfo shippingInfo = new ShippingInfo();
            shippingInfo.setOrderId(orderId);
            shippingInfo.setRecipientName(addresses.getName());
            shippingInfo.setPhoneNumber(addresses.getPhoneNumber());
            shippingInfo.setAddressId(materialRequest.getAddressId());

            log.info("【物流信息】开始保存物流信息，订单ID：{}，收货人：{}", orderId, addresses.getName());
            boolean shippingBool = shippingInfoService.save(shippingInfo);
            if (!shippingBool) {
                log.error("【物流信息】保存失败，订单ID：{}，地址ID：{}", orderId, materialRequest.getAddressId());
                return ResponseResult.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), "快递信息创建失败，请完善快递信息");
            }
            log.info("【物流信息】保存成功，物流记录ID：{}", shippingInfo.getShippingId());

            log.debug("【支付准备】开始获取订单信息，订单ID：{}", orderId);
            Orders byId = ordersService.getById(orderId);
            if (byId == null) {
                log.error("【支付准备】未找到对应订单，订单ID：{}", orderId);
                return ResponseResult.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), "订单信息异常");
            }

            log.debug("【支付准备】生成微信支付参数，订单ID：{}，金额：{}", orderId, byId.getTotalAmount());
            PrepayWithRequestPaymentResponse play = WxPlay.play(byId);

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("appId", play.getAppId());
            hashMap.put("nonceStr", play.getNonceStr());
            hashMap.put("packageVal", play.getPackageVal());
            hashMap.put("paySign", play.getPaySign());
            hashMap.put("signType", play.getSignType());
            hashMap.put("timeStamp", play.getTimeStamp());
            hashMap.put("orderId", orderId);

            log.info("【支付准备】成功生成支付参数，订单ID：{}，预支付ID：{}",
                    orderId,
                    play.getPackageVal().replace("prepay_id=", ""));
            return ResponseResult.success("", hashMap);

        } catch (Exception e) {
            log.error("【系统异常】处理发货申请时发生未预期错误，订单ID：{}，错误类型：{}，错误信息：{}，堆栈跟踪：",
                    orderId,
                    e.getClass().getSimpleName(),
                    e.getMessage(),
                    e);
            return ResponseResult.failure(
                    HttpStatus.INTERNAL_SERVER_ERROR.value(),
                    "订单生成失败: " + e.getMessage()
            );
        }
    }

    // 其他原有方法保持完全不变
    @PostMapping("/MaterialRequestList")
    public ResponseResult<PageResponse<MaterialRequest>> MaterialRequestList(PageSearchRequest request) {
        log.info("【发货申请】分页查询开始，查询参数：{}", request.toString());
        try {
            PageResponse<MaterialRequest> result = materialRequestService.getMaterialRequestMapper(request);
            log.info("【发货申请】分页查询成功，总记录数：{}", result.getTotal());
            return ResponseResult.success(result);
        } catch (Exception e) {
            log.error("【发货申请】分页查询异常，请求参数：{}，错误信息：{}",
                    request.toString(),
                    e.getMessage(),
                    e);
            return ResponseResult.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败");
        }
    }
}
=======
import com.jx.mlxg.entity.MaterialRequest;
import com.jx.mlxg.mapper.MaterialRequestMapper;
import com.jx.mlxg.service.MaterialRequestService;
import com.jx.mlxg.service.impl.MaterialRequestServiceImpl;
import com.jx.mlxg.utils.PageResponse;
import com.jx.mlxg.utils.PageSearchRequest;
import com.jx.mlxg.utils.ResponseResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/materialrequest")
public class MaterialRequestController {
    @Resource
    MaterialRequestServiceImpl materialRequestServiceImpl;

    @RequestMapping("/MaterialRequestList")
    public ResponseResult<PageResponse<MaterialRequest>> MaterialRequestList(PageSearchRequest request) {
        PageResponse<MaterialRequest> materialRequestMapper = materialRequestServiceImpl.getMaterialRequestMapper(request);
        return ResponseResult.success(materialRequestMapper);
    }
}
>>>>>>> eb72a42842a7e02f996c4f2d21dc4051aa14ecf1
